package com.guanyong.services;

import com.guanyong.dao.IFilmDaoMapper;
import com.guanyong.dao.IFilmInfoDaoMapper;
import com.guanyong.dao.IVideoScoreDaoMapper;
import com.guanyong.model.FilmInfoModel;
import com.guanyong.model.FilmModel;
import com.guanyong.model.PriceTagModel;
import com.guanyong.model.VideoScoreModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * Created by mxk on 2017/6/1.
 */
@Service
public class FilmServices implements IObjectUpdateScore ,IObjectUpdateCount{

    @Autowired
    IFilmDaoMapper iFilmDaoMapper;

    @Autowired
    IFilmInfoDaoMapper iFilmInfoDaoMapper;

    @Autowired
    IVideoScoreDaoMapper iVideoScoreDaoMapper;

    @Autowired
    PriceTagService priceTagService;

    // 获取电影对象
    public FilmModel GetFilmModelById(String fid){
        FilmModel filmModel=  iFilmDaoMapper.selectByPrimaryKey(fid);

        PriceTagModel priceTagModel=priceTagService.GetPriceTagByCache(filmModel.getfPricetagid());
        if (priceTagModel!=null && priceTagModel.getfId()!=null && !priceTagModel.getfId()
                .equals(""))
        {
            filmModel.setfPricetagname(priceTagModel.getfName());
        }
        else{
            filmModel.setfPricetagname("");
        }
        filmModel.setfFilmtypename(CommFunc.GetFilmTypeNameById(filmModel.getfFilmtype()));
        filmModel.setfCountryname(CommFunc.GetCountryNameById(filmModel.getfCountry()));
        return filmModel;
    }

    //获取电影对象
    public FilmInfoModel GetFilmInfoModelById(String fid){
        return  iFilmInfoDaoMapper.selectByPrimaryKey(fid);
    }

    public List<FilmModel> GetFilmModelByIdList(String idlist){
        List<FilmModel> filmModelList= iFilmDaoMapper.selectIdList(idlist);

        SetProperty(filmModelList);
        return filmModelList;
    }

    //获取电影列表
    public HashMap<String,Object> GetFilmList(
            FilmModel filmModel,
            Integer pno,
            Integer pagesize,
            Integer allcount){
        filmModel.setfFilmtype(filmModel.getfFilmtype().replace("0","_"));
        HashMap<String,Object> hashMap=new HashMap();
        if (allcount<0){
            Integer count=iFilmDaoMapper
                    .SelectFilmListCount(filmModel);
            hashMap.put("count",count);
        }
        else {
            hashMap.put("count",allcount);
        }
        List<FilmModel> filmModelList
                =iFilmDaoMapper.SelectFilmList(filmModel,(pno-1)*pagesize,pagesize);

        SetProperty(filmModelList);
        hashMap.put("list",filmModelList);
        return hashMap;
    }

    //获取电影列表
    public HashMap<String,Object> GetFilmList1_0(
            FilmModel filmModel,
            Integer pno,
            Integer pagesize,
            Integer allcount){
        HashMap<String,Object> hashMap=new HashMap();
        if (allcount<0){
            Integer count=iFilmDaoMapper
                    .SelectFilmListCount1_0(filmModel);
            hashMap.put("count",count);
        }
        else {
            hashMap.put("count",allcount);
        }
        List<FilmModel> filmModelList
                =iFilmDaoMapper.SelectFilmList1_0(filmModel,(pno-1)*pagesize,pagesize);

        SetProperty(filmModelList);
        hashMap.put("list",filmModelList);
        return hashMap;
    }


    //获取电影总数
    public Integer GetFilmAllCount(
         ){
        //huo去总数
        FilmModel filmModel=new FilmModel();
        filmModel.setfShow(0);
        Integer count=iFilmDaoMapper
                .SelectFilmListCount(filmModel);
        return count;
    }

    //获取电影列表
    public HashMap<String,Object> GetFilmBySubjectList(
            Integer Subjectid,
            Integer pno,
            Integer pagesize,
            Integer allcount){

        HashMap<String,Object> hashMap=new HashMap();
        if (allcount<0){
            Integer count=iFilmDaoMapper
                    .SelectBySubjectIdCount(Subjectid);
            hashMap.put("count",count);
        }
        else {
            hashMap.put("count",allcount);
        }
        List<FilmModel> filmModelList
                =iFilmDaoMapper.SelectBySubjectId(Subjectid,(pno-1)*pagesize,pagesize);

        SetProperty(filmModelList);
        hashMap.put("list",filmModelList);
        return hashMap;
    }

    public List<FilmModel> GetRandomFilmList(
            FilmModel filmModel,
            Integer datacount){
        filmModel.setfFilmtype(filmModel.getfFilmtype().replace("0","_"));
        filmModel.setfShow(0);
        Integer count=iFilmDaoMapper.SelectFilmListCount(filmModel);

        List<FilmModel> filmModelList=null;
        if (count>0) {
            Integer offset=0;
            if(count<=datacount){
                //noting is here
            }
            else {
                Random random = new Random();
                int maxoffset=count-datacount;
                offset=random.nextInt(maxoffset);
            }
            filmModelList
                    =iFilmDaoMapper.SelectFilmList(filmModel,offset,datacount);
        }
        else{
            filmModelList=new ArrayList<FilmModel>();
        }
        SetProperty(filmModelList);
        return filmModelList;
    }

    public List<FilmModel> GetRandomFilmList1_0(
            FilmModel filmModel,
            Integer datacount){
        filmModel.setfShow(0);
        Integer count=iFilmDaoMapper.SelectFilmListCount1_0(filmModel);

        List<FilmModel> filmModelList=null;
        if (count>0) {
            Integer offset=0;
            if(count<=datacount){
                //noting is here
            }
            else {
                Random random = new Random();
                int maxoffset=count-datacount;
                offset=random.nextInt(maxoffset);
            }
            filmModelList
                    =iFilmDaoMapper.SelectFilmList1_0(filmModel,offset,datacount);
        }
        else{
            filmModelList=new ArrayList<FilmModel>();
        }
        SetProperty(filmModelList);
        return filmModelList;
    }

    //获取最新的和最热的影片
    public List<FilmModel> GetNewsHotFilmList(Integer newsCount,Integer hotCount) {
        List<FilmModel>  filmModelList= iFilmDaoMapper.GetNewsHotFilmDaoList(newsCount,hotCount);

        SetProperty(filmModelList);
        return filmModelList;
    }


    public void  SetProperty(List<FilmModel> filmModelList){
        for (FilmModel filmModel1 :filmModelList){
            PriceTagModel priceTagModel=priceTagService.GetPriceTagByCache(filmModel1.getfPricetagid());
            if (priceTagModel!=null && priceTagModel.getfId()!=null && !priceTagModel.getfId()
                    .equals(""))
            {
                filmModel1.setfPricetagname(priceTagModel.getfName());
            }
            else{
                filmModel1.setfPricetagname("");
            }
            filmModel1.setfFilmtypename(CommFunc.GetFilmTypeNameById(filmModel1.getfFilmtype()));
            filmModel1.setfCountryname(CommFunc.GetCountryNameById(filmModel1.getfCountry()));
        }
    }


    //获取最新的和最热的影片
    public List<FilmModel> GetNewsFilmList(Integer newsCount,Integer hotCount) {
        return  iFilmDaoMapper.GetNewsHotFilmDaoList(newsCount,hotCount);
    }
    //获取首页电影列表
    public HashMap<String ,List<FilmModel>> GetFilmTypeIdList(String typeIdlistStr, Integer count){
        String[] typeAry=typeIdlistStr.split(",");
        HashMap<String ,List<FilmModel>> hashMap=new HashMap<String ,List<FilmModel>>();
        HashMap<String,Integer> typeHash=new HashMap<String, Integer>();
        String idlist="";
        for ( String typeid: typeAry) {
            Integer typeIdN=Integer.parseInt(typeid);
            String typeIdlist=getTypeIdStrList(typeIdN);
            typeHash.put(typeIdlist,typeIdN);
            if (!hashMap.containsKey(typeid)){

                List<FilmModel> filmModelList=iFilmDaoMapper.selectFilmTypeNoRepateIdList
                        (typeIdlist,idlist,count);

                for (FilmModel filmModel:filmModelList){
                    idlist+=",'"+filmModel.getfId().toString()+"'";
                }
                if (idlist.startsWith(",")){
                    idlist=idlist.substring(1);
                }
                hashMap.put(typeid,filmModelList);

            }
        }
//        List<FilmModel> filmModelList=iFilmDaoMapper.selectFilmTypeIdList(typeHash,count);
//        for (FilmModel filmModel:
//        filmModelList) {
//            String kesy=filmModel.getfMakelookct().toString();
//            if (hashMap.containsKey(kesy)){
//                hashMap.get(filmModel.getfMakelookct().toString()).add(filmModel);
//            }
//
//        }
//        SetProperty(filmModelList);
        return hashMap;
    }

    private String getTypeIdStrList(Integer typeIndex){
        StringBuilder stringBuilder=new StringBuilder();
        for (Integer i=0; i<19;i++){
            if (typeIndex==i){
                stringBuilder.append("1");
            }
            else{
                stringBuilder.append("_");
            }

        }
        return  stringBuilder.toString();
    }



    @Override
    public Integer UpdateObjectScore(String objectid, Double score){
        FilmModel filmModel =new FilmModel();
        filmModel.setfScore(score);
        filmModel.setfId(objectid);
        return  UpdateFilmSingle(filmModel);
    }



    public Integer UpdateCommentCount(String objectid){

        return  iFilmDaoMapper.UpdateCommentCount(objectid);
    }


    public  Integer UpdateFilmSingle(FilmModel filmModel){
           return   iFilmDaoMapper.update(filmModel);
    }

    //修改
    public  Integer UpdateFilm(FilmModel filmModel, FilmInfoModel filmInfoModel){
        Integer res=  iFilmDaoMapper.update(filmModel);
        if (res >0){
            res=iFilmInfoDaoMapper.update(filmInfoModel);

        }
        else{
            res=-2;
        }
        return res;
    }
    //添加
    public  Integer InsertFilm(FilmModel filmModel, FilmInfoModel filmInfoModel,VideoScoreModel videoScoreModel){
        Double sumScore=0.0;
        int temp=0;
        if (videoScoreModel.getfDouban()>0){
            sumScore+=videoScoreModel.getfDouban();
            temp++;
        }
        if (videoScoreModel.getfImdb()>0){
            sumScore+=videoScoreModel.getfImdb();
            temp++;
        }
        if (videoScoreModel.getfLanfanqie()>0){
            sumScore+=videoScoreModel.getfLanfanqie();
            temp++;
        }
        if (temp!=0){
            sumScore=sumScore/temp;
            BigDecimal bg = new BigDecimal(sumScore).setScale(2, RoundingMode.UP);
            filmModel.setfScore(bg.doubleValue());
        }
        else{
            filmModel.setfScore(0.0);
        }
        Integer res=   iFilmDaoMapper.insert(filmModel);
        if (res >0){
            res=iFilmInfoDaoMapper.insert(filmInfoModel);
            if (res>-1){

                res =iVideoScoreDaoMapper.insert(videoScoreModel);
                if (res>-1){
                    //
                }
                else{
                    res=-4;
                }
            }
            else{
                res=-3;
            }
        }
        else{
            res=-2;
        }
        return res;
    }

    @Override
    public Integer UpdateObjectLookCount(String objectid) {
       return  iFilmDaoMapper.updatelookct(objectid);
    }

    @Override
    public Integer UpdateObjectGoodCount(String objectid) {

        return  iFilmDaoMapper.updategoodct(objectid);
    }

    @Override
    public Integer UpdateObjectShareCount(String objectid) {

        return  iFilmDaoMapper.updatesharect(objectid);

    }

    @Override
    public Integer UpdateObjectPlayCount(String objectid) {

        return  iFilmDaoMapper.updateplayct(objectid);

    }

    @Override
    public Integer UpdateObjectCollectCountAdd(String objectid) {

        return  iFilmDaoMapper.updatecollectct(objectid);
    }

    @Override
    public  Integer UpdateObjectCollectCountdel(String objectid){
        return iFilmDaoMapper.updatecollectctdel(objectid);
    }

    public  Integer UpdateObjectCollectCountdelList(String idlist){
        return iFilmDaoMapper.updatecollectctdellist(idlist);
    }

    @Override
    public Integer UpdateObjectBuyCount(String objectid) {

        return  iFilmDaoMapper.updatebuyct(objectid);
    }



    public HashMap<String,Object> GetByfPlayctFilmList(Integer limit){
        HashMap<String,Object> hashMap=new HashMap();

        List<FilmModel> filmModelList
                =iFilmDaoMapper.SelecByfPlayctFilmList(0,limit);

        SetProperty(filmModelList);
        hashMap.put("list",filmModelList);
        return hashMap;
    }

    public Integer UpdateFilmStagephoto(String fid,String fStagephoto) {
        return iFilmDaoMapper.UpdateFilmStagephoto(fid,fStagephoto);
    }
}
